package com.liholin.springboot.sample.service.impl;

import com.liholin.springboot.sample.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.TimeUnit;


@Service
public class RedisServiceImpl implements RedisService {

    @Value("${redisDefaultKey}")
    private String redisFixKey;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    RedisTemplate<String, String> redisTemplate;


    /**
     * 存入redis中的key
     *
     * @return
     */
    private String getRedisKey(String key) {
        return redisFixKey + ":" + key;
    }

    /**
     * 保存
     *
     * @param key
     * @param value
     */
    public void put(String key, String value, long timeSeconds) {
        if (isExists(getRedisKey(key))) {
            delete(key);
        }
        stringRedisTemplate.opsForValue().set(getRedisKey(key), value, timeSeconds, TimeUnit.SECONDS);
    }
    public void put(String key, String value) {
        if (isExists(getRedisKey(key))) {
            delete(key);
        }
        stringRedisTemplate.opsForValue().set(getRedisKey(key), value);
    }

    /**
     * 保存（没有过期时间）
     *
     * @param key
     * @param value
     */
    public void save(String key, String value) {
        stringRedisTemplate.opsForValue().set(getRedisKey(key), value);
    }

    /**
     * 存储Hash值
     *
     * @param key
     * @param map
     */
    @Override
    public void HPutMap(String key, Map<String, Object> map) {
        stringRedisTemplate.opsForHash().putAll(getRedisKey(key), map);
    }

    /**
     * 修改指定Key的指定哈希Key的Value值
     *
     * @param key
     * @param hKey
     * @param hValue
     */
    @Override
    public void HUpdateSmartAlbum(String key, String hKey, Object hValue) {
        stringRedisTemplate.opsForHash().put(getRedisKey(key), hKey, hValue);
    }

    @Override
    public Map HGetByKey(String key) {
        return stringRedisTemplate.opsForHash().entries(getRedisKey(key));
    }

    @Override
    public void HDeleteByHKey(String key, String hashKeys) {
        stringRedisTemplate.opsForHash().delete(getRedisKey(key), hashKeys);
    }


    /**
     * 删除
     *
     * @param key
     */
    public void delete(String key) {
        stringRedisTemplate.delete(getRedisKey(key));
    }

    /**
     * 判断是否存在某个key
     *
     * @param key
     * @return
     */
    public boolean isExists(String key) {
        return stringRedisTemplate.hasKey(getRedisKey(key));
    }

    /**
     * 根据key获取
     *
     * @param key
     * @return
     */
    public String get(String key) {
        return stringRedisTemplate.opsForValue().get(getRedisKey(key));
    }

    /**
     * Hash存储
     *
     * @param key
     * @param hk
     * @param hv
     */
    public void HPut(String key, Object hk, Object hv) {
        if (redisTemplate.opsForHash().hasKey(getRedisKey(key), hk)) {
            redisTemplate.opsForHash().delete(getRedisKey(key), hk);
        }
        redisTemplate.opsForHash().put(getRedisKey(key), hk, hv);
    }

    /**
     * Hash删除
     *
     * @param key
     * @param hk
     */
    public void HDelete(String key, Object hk) {
        redisTemplate.opsForHash().delete(getRedisKey(key), hk);
    }

    /**
     * Hash获取
     *
     * @param key
     * @param hk
     * @return
     */
    public Object HGet(String key, Object hk) {
        return redisTemplate.opsForHash().get(getRedisKey(key), hk);
    }

    @Override
    public Map<String, String> HGetAll(String key) {

        BoundHashOperations boundHashOperations = redisTemplate.boundHashOps(getRedisKey(key));
        Map<String, String> dataMap = boundHashOperations.entries();

        return dataMap;
    }
}
